﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace W.Product.Repository
{
    public class BaseRepository<T> : IRepository<T> where T : class, new()
    {

        private readonly EFProductDbContext db;
        public BaseRepository(EFProductDbContext db) { 
            this.db = db;
        }
        public async Task<int> Add(T entity)
        {
             db.Set<T>().AddAsync(entity);   
            return await db.SaveChangesAsync();
        }

        public async Task<int> Delete(int id)
        {
             db.Remove(id);
            return await db.SaveChangesAsync();
        }

        public async Task<T> FindById(int id)
        {
          return await  db.Set<T>().FindAsync(id);

        }

        public async Task<List<T>> GetAll()
        {
            return await db.Set<T>().ToListAsync();
        }

        public async Task<int> IsUpdate(List<T> values)
        {
              db.UpdateRange(values);
            return await db.SaveChangesAsync();
        }

        public async Task<int> Update(T entity)
        {
            db.Update(entity);
            return await db.SaveChangesAsync();
        }
    }
}
